package com.cskaoyan.service;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.BrandVo;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class AdminBrandServiceImpl implements AdminBrandService {
    @Override
    public CommonData<MarketBrand> query(Integer page, Integer limit, String sort, String order, String username, Integer id) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringUtil.isEmpty(username)) {
            criteria.andNameEqualTo(username);
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        MarketBrandMapper marketBrandMapper = TransactionUtil.getMapper(MarketBrandMapper.class);
        PageHelper.startPage(page, limit);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(example);
        return CommonData.data(new PageInfo(marketBrands));
    }

    @Override
    public BaseRespVo delete(MarketBrand marketBrand) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        MarketBrandMapper marketBrandMapper = TransactionUtil.getMapper(MarketBrandMapper.class);
        Integer id = marketBrand.getId();
        MarketBrand marketBrandDelete = marketBrandMapper.selectByPrimaryKey(id);
        marketBrandDelete.setDeleted(true);
        marketBrandMapper.updateByPrimaryKey(marketBrandDelete);

        TransactionUtil.commit();

        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo update(MarketBrand marketBrand) {
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        MarketBrandMapper marketBrandMapper = TransactionUtil.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrandUpdate = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());

        marketBrandUpdate.setAddTime(new Date());
        marketBrandUpdate.setUpdateTime(new Date());
        marketBrandUpdate.setName(marketBrand.getName());
        marketBrandUpdate.setFloorPrice(marketBrand.getFloorPrice());
        marketBrandUpdate.setPicUrl(marketBrand.getPicUrl());
        marketBrandUpdate.setDesc(marketBrand.getDesc());

        marketBrandMapper.updateByPrimaryKey(marketBrandUpdate);
        TransactionUtil.commit();

        return BaseRespVo.ok();
    }

    @Override
    public BaseRespVo create(BrandVo brandVo) {
        BaseRespVo error = validate(brandVo);
        if (error != null) {
            return error;
        }
        String name = brandVo.getName();
        if (this.checkExistByName(name)) {
            return BaseRespVo.fail(611, "商品名已经存在");
        }
        MarketBrandMapper marketBrandMapper = TransactionUtil.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setDesc(brandVo.getDesc());
        marketBrand.setFloorPrice(brandVo.getFloorPrice());
        marketBrand.setName(brandVo.getName());
        marketBrand.setPicUrl(brandVo.getPicUrl());
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());
        marketBrandMapper.insertSelective(marketBrand);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andNameEqualTo(brandVo.getName());
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        TransactionUtil.commit();
        return BaseRespVo.ok(marketBrands.get(0));
    }

    @Override
    public boolean checkExistByName(String name) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketBrandMapper marketBrandMapper = sqlSession.getMapper(MarketBrandMapper.class);
        MarketBrandExample example = new MarketBrandExample();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        boolean b = marketBrandMapper.countByExample(example) != 0;
        sqlSession.close();
        return b;
    }

    private BaseRespVo validate(BrandVo brandVo) {

        String name = brandVo.getName();
        if (StringUtil.isEmpty(name)) {
            return BaseRespVo.badArgument();
        }
        return null;
    }
}
